package com.drophair.jspwork.Servlet;

import com.drophair.jspwork.bean.SqlConnect;
import com.drophair.jspwork.testtools.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Vector;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        doGet(request,response);
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

//        System.out.println(request.getParameter("login_id") +"\n" + request.getParameter("login_pwd"));
        String login_id=request.getParameter("login_id");
        String login_pwd=request.getParameter("login_pwd");
        String login_name="";
        String uwho="";
        SqlConnect sqlConnect=new SqlConnect();

        HttpSession session = request.getSession();

        System.out.println(login_id + ' ' + login_pwd);
        if((login_id==null||login_id.equals(""))||(login_pwd==null||login_pwd.equals(""))){
//            session.setAttribute("error","账号和密码不得为空");
            request.setAttribute("error","账号和密码不得为空");
            request.getRequestDispatcher("main.jsp").forward(request,response);
        }
        else{
            Cookie cookie=new Cookie("Userid",login_id);
            Cookie cookie1=new Cookie("Upasswd",login_pwd);

            response.addCookie(cookie);
            response.addCookie(cookie1);
            try {
                Vector<String> info=sqlConnect.login(login_id,login_pwd);
                Debug.consle_print(Integer.toString(info.size()));
                if(info.size()>0) {
                    Debug.consle_print(login_id);
                    login_name=info.get(0);
                    System.out.println("LOGIN_NAME: "+login_name);
                    uwho=info.get(1);
                    System.out.println("uWHO: "+uwho);
                    Cookie cookie2=new Cookie("login_name",login_name);
                    Cookie cookie3=new Cookie("uwho",uwho);
                    response.addCookie(cookie2);
                    response.addCookie(cookie3);
                    response.sendRedirect("index.jsp");
                    Debug.consle_print("OK");
//                    response.sendRedirect("login_success.jsp");
//                    request.getRequestDispatcher("login_success.jsp").forward(request, response);
                }else{
                    request.setAttribute("error","账号或密码错误");
                    request.getRequestDispatcher("login.jsp").forward(request,response);
                    Debug.consle_print("fail");
//                    request.getRequestDispatcher("main.jsp").forward(request,response);
                }
//            response.sendRedirect("login_success.jsp");
            } catch (ClassNotFoundException e) {
                System.out.println("ERROR1");
                e.printStackTrace();
            } catch (SQLException throwables) {
                System.out.println("ERROR2");
                throwables.printStackTrace();
            }
        }
    }
}
